package com.mallkit.core.helpers
{
	import com.mallkit.core.events.SelectMainViewEvent;
	import com.mallkit.core.view.baseViews.ISubView;
	
	import mx.containers.ViewStack;
	
	/**
	 * All static class that provides a shared use functions for
	 * the application main views and sub views
	 * 
	 * @see com.mallkit.core.view.baseViews.MainViewBase
	 * @see com.mallkit.core.view.baseViews.SubViewBase
	 */
	public class MainViewsHelper
	{
		/**
		 * Selects the required sub view in a view stack. To be called by Main Views
		 * 
		 * @param viewStack <code>ViewStack</code> object
		 * @param subViewName name of subview to choose
		 * @param resetSubView if true it will reset the new sub view
		 * to it's original state
		 */
		public static function selectSubViewInViewStack(viewStack:ViewStack ,subViewName:String, resetSubView:Boolean):void
		{
			if (viewStack == null)
			{
				viewStack = new ViewStack();
			}
			
			var mainViewsCount:int = viewStack.numChildren;
			var i:int;
			var view:ISubView;
			var found:Boolean = false;
			
			if (subViewName != "")
			{
				for (i=0; i < mainViewsCount; i++)
				{
					view = viewStack.getChildAt(i) as ISubView;
					if (view.getSubViewName() == subViewName)
					{
						viewStack.selectedIndex = i;
						found = true;
						break;
					}
				}
			}
			
			if (view != null && resetSubView)
			{
				view.reset();
			}
		}
		
		/**
		 * Use to show a view/sub view on screen
		 * 
		 * @param mainView name of main view to select
		 * @param subView name of sub view to select
		 * @param reset If true, it will reset the new view selected to it's original state
		 */
		public static function dispatchSelectViewEvent(mainView:String = "", subView:String = "", reset:Boolean = false):void
		{
			var event:SelectMainViewEvent = new SelectMainViewEvent();
			event.mainView = mainView;
			event.subView = subView;
			event.resetView = reset;
			event.dispatch();
		}
	}
}